$(function(){
	//请求数据
	createMainTable({});


	//搜索确认按钮
	$("#confirmBtn").click(function(){
		var data = {
			beginDate : $('#selectorBeginDateShow').html(),
			endDate : $('#selectorEndDateShow').html(),
			qryRecSubject: $('#qryRecSubject').val(),
			qryRecWare: $('#qryRecWare').val(),
			qryType: $('#selectorPayShow').attr('data-value'), //收付类型
			qryPaySubject: $('#qryPaySubject').val(),
			qryPayWare: $('#qryPayWare').val(),
			qryState: $('#selectorClearShow').attr('data-value'), //清算状态
		}
		createMainTable(data); //请求数据
		mui.back(); //mui 返回
	});


	//选择器
	$(".myPicker").click(function(){
		var self = this;
		var nowDate = new Date();
		//创建普通的popPicker
		var popPicker = new mui.PopPicker();
		var popDataArray = new Array();
		//创建日期形式的Depicker
		var datePicker = new mui.DtPicker({
			type: "date",
			endDate: nowDate
		});
		switch ($(self).attr('id')){
			//开始日期选择器
			case 'selectorBeginDate':
				datePicker.setSelectedValue(getMonthStartDate());
				datePicker.show(function(items) {
					$("#selectorBeginDateShow").html(items.value);
					datePicker.dispose();
				});
				break;
			//结束日期选择器
			case 'selectorEndDate':
				datePicker.show(function(items) {
					$("#selectorEndDateShow").html(items.value);
					datePicker.dispose();
				});
				break;
			//收付类型
			case 'selectorPay':
				popDataArray = [];//将数组清空
				popDataArray = [{value: '' ,text: '全部'},{value: '1' ,text: '付款'},{value: '0' ,text: '收款'}];
				popPicker.setData(popDataArray);
				popPicker.show(function(items) {
					$("#selectorPayShow").html(items[0].text).attr('data-value',items[0].value);
					popPicker.dispose();
				});
				break;
			//清算状态
			case 'selectorClear':
				popDataArray = [];//将数组清空
				popDataArray = [{value: '' ,text: '全部'},{value: '1' ,text: '已清'},{value: '0' ,text: '未清'}];
				popPicker.setData(popDataArray);
				popPicker.show(function(items) {
					$("#selectorClearShow").html(items[0].text).attr('data-value',items[0].value);
					popPicker.dispose();
				});
				break;
			default:
				break;
		}
	});
})

//获取往来报表数据
function createMainTable(params){
	params = params || {};
	//显示当前查询条件
	$("#qryRecSubjectC").html(params.qryRecSubject || '');
	$("#qryRecWareC").html(params.qryRecWare || '');
	$("#qryPaySubjectC").html(params.qryPaySubject || '');
	$("#qryPayWareC").html(params.qryPayWare || '');
	$("#qryTypeC").html($('#selectorPayShow').html());
	$("#qryStateC").html($('#selectorClearShow').html());
	$("#beginDate").html(params.beginDate || '');
	$("#endDate").html(params.endDate || '');


	$('#table').html("");
	layui.use('table', function() {
		var table = layui.table;
		table.render({
			elem: '#table'
			, limit: 20
			// ,skin: 'line'
			, even: true
			, size: 'sm'
			, height: $(window).height() - $('.head-search').height() - 120
			, url: url1 + Utils.Api.rptWlGet //数据接口
			, where: {
				accessid: window.localStorage.accessid || '',
				field: 'recSubjectNick',
				direction: 'ASC',
				qryDateB: params.beginDate || '',
				qryDateE: params.endDate || '',
				qryRecSubject: params.qryRecSubject || '',
				qryRecWare: params.qryRecWare || '',
				qryType: params.qryType || '', //收付类型
				qryPaySubject: params.qryPaySubject || '',
				qryPayWare: params.qryPayWare || '',
				qryState: params.qryState || '', //清算状态
			}
			, request: {
				pageName: 'pageIndex' //页码的参数名称，默认：page
				, limitName: 'limit' //每页数据量的参数名，默认：limit
			}
			, response: {
				statusName: 'hasError', //数据状态的字段名称，默认：code
				statusCode: false, //成功的状态码，默认：0
				msgName: 'error', //状态信息的字段名称，默认：msg
				countName: 'results', //数据总数的字段名称，默认：count
				dataName: 'rows' //数据列表的字段名称，默认：data
			}
			, page: true //开启分页
			, cols: [[ //表头
				{field: 'subjectNW', title: '收付类型', align: 'center', width: 80, templet: function (d) {
					return d.subjectNW === 0? '<span class="color-green">收款</span>' : '<span class="color-red">付款</span>'
				}},
				{field: 'recSubjectNick', title: '收款单位及单元', align: 'center', width: 180, templet: function(d){
					return d.recSubjectNick + '/' + d.recWareNick
				}},
				{field: 'resName', title: '付款单位及单元', align: 'center', width: 180, templet: function(d){
					return d.paySubjectNick + '/' + d.payWareNick
				}},
				{field: 'accUnQsAmount', title: '未清金额', align: 'right', width: 100, templet: function (d) {
					return Utils.formatMoney(d.accUnQsAmount);
				}},
				{field: '', title: '操作', templet: '#titleTpl', align: 'center', width: 100},
			]]
			,
		});
	})
}


function Detail(accRecvSubjectId, accRecvWareId, accPaySubjectId, accPayWareId){
	//往来明细固定数据
	var detailParams = {
		accRecvSubjectId: accRecvSubjectId,
		accRecvWareId: accRecvWareId,
		accPaySubjectId: accPaySubjectId,
		accPayWareId: accPayWareId,
	}
	mui.openWindow({
		url: 'dealingsDetail.html',
	})
	window.localStorage.wlParams = JSON.stringify(detailParams)
}